package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.constant.OrderConstant;
import com.webfinal.backsystem.dao.*;
import com.webfinal.backsystem.entity.*;
import com.webfinal.backsystem.request.order.*;
import com.webfinal.backsystem.response.order.*;
import com.webfinal.backsystem.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private final static Logger LOG= LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private OrderDetailViewMapper orderDetailViewMapper;

    @Autowired
    private OrderViewMapper orderViewMapper;

    @Autowired
    private OrderProductsMapper orderProductsMapper;

    @Override
    @Transactional
    public PlaceOrderResponse placeOrder(PlaceOrderRequest request) throws Exception {
        PlaceOrderResponse response=new PlaceOrderResponse();
        response.setResultCode("0");
        Order order=new Order();
        order.setOrderId(0);
        order.setOrderState(OrderConstant.ORDER_STATE_TYPE0);
        order.setInitTime(new Date());
        order.setUserId(request.getUserId());
        int temp=orderMapper.insert(order);
        if (temp!=1){
            response.setResultCode("1");
            response.setResultMsg("订单插入失败");
            LOG.error("OrderServiceImpl.placeOrder执行异常");
            throw new Exception("订单插入失败");
        }
        List<OrderDetail> orderDetailList=request.getOrderDetailList();
        for (OrderDetail item : orderDetailList) {
            item.setOrderId(order.getOrderId());
            item.setOrderState(OrderConstant.ORDER_STATE_TYPE0);
            int tmp=orderDetailMapper.insert(item);
            if (tmp!=1){
                response.setResultCode("1");
                response.setResultMsg("订单明细插入失败");
                throw new Exception("订单明细插入失败");
            }
        }
        if (response.getResultCode()=="0") {
            response.setOrder(order);
            response.setOrderDetailList(orderDetailList);
        }
        return response;
    }

    @Override
    @Transactional
    public OrderPayResponse orderPay(OrderPayRequest request) throws Exception{
        OrderPayResponse response=new OrderPayResponse();
        Payment payment=new Payment();
        payment.setOrderId(request.getOrderId());
        payment.setPaymentAccount(payment.getPaymentAccount());
        payment.setPaymentId(0);
        payment.setPaymentMethod(request.getPaymentMethod());
        payment.setPaymentDate(new Date());
        payment.setPaymentUser(request.getPaymentUser());
        int temp = paymentMapper.insert(payment);
        if (temp == 1) {
            Order order = new Order();
            order.setOrderId(request.getOrderId());
            order.setOrderState(OrderConstant.ORDER_STATE_TYPE1);
            order.setPayTime(new Date());
            int tmp0=orderMapper.updateByPrimaryKey(order);
            if (tmp0!=1) {
                throw new Exception("更新订单状态失败");
            }
            order = orderMapper.selectByPrimaryKey(request.getOrderId());
            response.setOrder(order);
            List<OrderDetail> orderDetailList=orderDetailMapper.selectByOrderId(request.getOrderId());
            for (OrderDetail item : orderDetailList) {
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setOrderState(OrderConstant.ORDER_STATE_TYPE1);
                orderDetail.setOrderDetailId(item.getOrderDetailId());
               int tmp1 = orderDetailMapper.updateByPrimaryKey(orderDetail);
                if (tmp1!=1) {
                    throw new Exception("更新订单状态失败");
                }
            }
            orderDetailList=orderDetailMapper.selectByOrderId(request.getOrderId());
            response.setOrderDetailList(orderDetailList);
        }
        else {
            Order order = new Order();
            order.setOrderId(request.getOrderId());
            order.setOrderState(OrderConstant.ORDER_STATE_TYPE2);
            orderMapper.updateByPrimaryKey(order);
        }
        response.setResultCode("0");
        response.setResultMsg("支付成功");
        return response;
    }

    @Override
    public DeliverGoodsResponse deliverGoods(DeliverGoodsRequest request) {
        DeliverGoodsResponse response = new DeliverGoodsResponse();
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(request.getOrderDetailId());
        if(orderDetail!=null){
            if(orderDetail.getOrderState().equals("1")||orderDetail.getOrderState().equals("3"))
            orderDetail.setOrderState("4");
            if(orderDetailMapper.updateByPrimaryKey(orderDetail)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }else {
                response.setResultCode("1");
                response.setResultMsg("error");
            }
        }else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }
        return response;
    }

    @Override
    public TakeDeliveryResponse takeDelivery(TakeDeliveryRequest request) {
        TakeDeliveryResponse response =new TakeDeliveryResponse();
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setOrderDetailId(request.getOrderDetailId());
        orderDetail.setOrderState(OrderConstant.ORDER_STATE_TYPE5);
        int temp = orderDetailMapper.updateByPrimaryKey(orderDetail);
        if (temp ==1) {
            response.setResultCode("0");
            response.setResultMsg("已收货");
        }
        else {
            response.setResultCode("1");
            response.setResultMsg("订单状态更新失败，稍后再试");
        }
        return response;
    }

    @Override
    public RefundApplyResponse refundApply(RefundApplyRequest request) {
        RefundApplyResponse response=new RefundApplyResponse();
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setOrderDetailId(request.getOrderDetailId());
        orderDetail.setOrderState(OrderConstant.ORDER_STATE_TYPE6);
        int temp = orderDetailMapper.updateByPrimaryKey(orderDetail);
        if (temp==1){
            response.setResultCode("0");
            response.setResultMsg("申请成功，等待审核");
        }
        else {
            response.setResultCode("1");
            response.setResultMsg("申请失败");
        }
        return response;
    }

    @Override
    public RefundApplyResponse refundAudit(RefundAuditRequest request) {
        RefundApplyResponse response =new RefundApplyResponse();
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(request.getOrderDetailId());
        if(orderDetail!=null){
            if(orderDetail.getOrderState().equals("6"))
                orderDetail.setOrderState("7");
            if(orderDetailMapper.updateByPrimaryKey(orderDetail)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }else {
                response.setResultCode("1");
                response.setResultMsg("error");
            }
        }else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }
        return response;
    }

    @Override
    public RefundResponse refund(RefundRequest request) {
        RefundResponse response = new RefundResponse();
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(request.getOrderDetailId());
        if(orderDetail!=null){
            if(orderDetail.getOrderState().equals("7")){
                orderDetail.setOrderState("9");
            }
             else {
                response.setResultCode("1");
                response.setResultMsg("error");
                return response;
            }

            if(orderDetailMapper.updateByPrimaryKey(orderDetail)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }else {
                response.setResultCode("1");
                response.setResultMsg("error");
            }
        }else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }
        return response;
    }

    @Override
    public QueryOrderResponse queryOrder(QueryOrderRequest request) {
        QueryOrderResponse response = new QueryOrderResponse();
        List<OrderAndDetail> orderAndDetailList=new ArrayList<>();
        List<Order> orderList=orderMapper.selectByUserId(request.getUserId());
        for (Order item : orderList) {
            OrderAndDetail orderAndDetail=new OrderAndDetail();
            List<OrderDetail> orderDetailList=orderDetailMapper.query(item.getOrderId(),request.getQueryFlag());
            if (orderDetailList!=null&&orderDetailList.size()>0) {
                orderAndDetail.setOrderDetailList(orderDetailList);
                orderAndDetail.setOrder(item);
                orderAndDetailList.add(orderAndDetail);
            }
        }
        if (orderAndDetailList.size() > 0){
            response.setOrderAndDetailList(orderAndDetailList);
            response.setResultCode("0");
            response.setResultMsg("");
        }
        else {
            response.setOrderAndDetailList(orderAndDetailList);
            response.setResultCode("0");
            response.setResultMsg("没查到相关信息");
        }
        return response;
    }

    @Override
    public OrderinfoResponse deliverOrder() {
        OrderinfoResponse response = new OrderinfoResponse();
        List<OrderDetailView> orderDetailViewList = orderDetailViewMapper.selectByState("1");
        orderDetailViewList.addAll(orderDetailViewMapper.selectByState("3"));
        if(orderDetailViewList==null){
            response.setResultCode("1");
            response.setResultMsg("未查到信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setOrderDetailViewList(orderDetailViewList);
        }
        return response;

    }

    @Override
    public OrderinfoResponse refundAuditOrder() {
        OrderinfoResponse response = new OrderinfoResponse();
        List<OrderDetailView> orderDetailViewList = orderDetailViewMapper.selectByState("6");
        if(orderDetailViewList==null){
            response.setResultCode("1");
            response.setResultMsg("未查到信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setOrderDetailViewList(orderDetailViewList);
        }
        return response;
    }

    @Override
    public OrderinfoResponse refundOrder() {
        OrderinfoResponse response = new OrderinfoResponse();
        List<OrderDetailView> orderDetailViewList = orderDetailViewMapper.selectByState("7");
        if(orderDetailViewList==null){
            response.setResultCode("1");
            response.setResultMsg("未查到信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setOrderDetailViewList(orderDetailViewList);
        }
        return response;
    }

    @Override
    public UpdateOrderDetailResponse updatedeliverOrder(UpdateOrderDetailRequest updateOrderDetailRequest) {
        UpdateOrderDetailResponse response = new UpdateOrderDetailResponse();
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(updateOrderDetailRequest.getOrderDetailId());
        if(orderDetail==null){
            response.setResultCode("1");
            response.setResultMsg("error");
            return response;
        }else {
            orderDetail.setOrderState("4");
            if(orderDetailMapper.updateByPrimaryKey(orderDetail)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }
            else {
                response.setResultCode("1");
                response.setResultMsg("error");
            }
        }
        return response;
    }

    @Override
    public UpdateOrderDetailResponse updaterefundAuditOrder(UpdateOrderDetailRequest updateOrderDetailRequest) {
        UpdateOrderDetailResponse response = new UpdateOrderDetailResponse();
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(updateOrderDetailRequest.getOrderDetailId());
        if(orderDetail==null){
            response.setResultCode("1");
            response.setResultMsg("error");
            return response;
        }else {
            orderDetail.setOrderState("7");
            if(orderDetailMapper.updateByPrimaryKey(orderDetail)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }else{
                response.setResultCode("1");
                response.setResultMsg("error");
            }

        }
        return response;
    }

    @Override
    public UpdateOrderDetailResponse updaterefundOrder(UpdateOrderDetailRequest updateOrderDetailRequest) {
        UpdateOrderDetailResponse response = new UpdateOrderDetailResponse();
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(updateOrderDetailRequest.getOrderDetailId());
        if(orderDetail==null){
            response.setResultCode("1");
            response.setResultMsg("error");
            return response;
        }else {
            orderDetail.setOrderState("9");
            if(orderDetailMapper.updateByPrimaryKey(orderDetail)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }else{
                response.setResultCode("1");
                response.setResultMsg("error");
            }

        }
        return response;
    }

    @Override
    public SearchOrderResponse searchorder(SearchOrderRequest searchOrderRequest) {
        SearchOrderResponse response=new SearchOrderResponse();
        List<OrderView> orderViewList = orderViewMapper.selectByusername(searchOrderRequest.getUsername());
        if(orderViewList==null){
            response.setResultCode("1");
            response.setResultMsg("未查到信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setOrderViewList(orderViewList);
        }
        return response;
    }

    @Override
    public SearchOrderResponse searchallorder() {
        SearchOrderResponse response=new SearchOrderResponse();
        List<OrderView> orderViewList = orderViewMapper.selectAll();
        if(orderViewList==null){
            response.setResultCode("1");
            response.setResultMsg("未查到信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setOrderViewList(orderViewList);
        }
        return response;
    }

    @Override
    public OrderProductsResponse orderproducts(OrderProductsResquest orderProductsResquest) {
        OrderProductsResponse response = new OrderProductsResponse();
        List<OrderProducts> orderProducts=orderProductsMapper.selectByOrderid(orderProductsResquest.getOrderId());
        if(orderProducts==null){
            response.setResultCode("1");
            response.setResultMsg("未查到信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setOrderProductsList(orderProducts);
        }
        return response;
    }

    @Override
    public OrderProductsResponse orderproductsall() {
        OrderProductsResponse response = new OrderProductsResponse();
        List<OrderProducts> orderProducts=orderProductsMapper.selectAll();
        if(orderProducts==null){
            response.setResultCode("1");
            response.setResultMsg("未查到信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setOrderProductsList(orderProducts);
        }
        return response;
    }

}
